#!/usr/bin/python
from pftpc import Queue
from pftpc import DBQueue
"""QueueTest - Test Queues."""
def pack_int(i):
	"""Return in as a string."""
	return str(i)
def unpack_int(i):
	"""convert string to an int."""
	return int(i)
def construct_q(N):
	"""Construct a Queue containing 0 to 'N` or empty if 'N` is none."""
	if N:
		buffer = range(0,N)
		return Queue(buffer)
	return Queue()
def construct_dbq(N,db=None):
	"""Construct a DBQueue containing 0 to 'N` or empty if N is none."""
	if N:
		buffer = range(0,N)
		return DBQueue(pack_int,unpack_int,db,buffer)
	return DBQueue(pack_int,unpack_int,none,none)

def test_len(q, N):
	"""Test len() function."""
	if not (len(q)==N):
		print "Length of Queue(%i) does not match N(%i)."%(len(q),N)
def test_iter(q, N):
	"""Test Iter() function."""
	count = -1;
	for x in q:
		count = count+1
		if not (x == count):
			print "Iteration number %i returned %i"%(count,x)
			return
def test_getitem(q, N):
	"""Test get using [] notation."""
	for x in range(N):
		if not (x == q[x]):
			print "Error retriveing element at %i, returned %i."%(x, q[x])
			return
def test_setitem(q):
	"""Test setitem."""
	for x in range(N):
		q[x]=chr(x)
	for x in range(N):
		if not (chr(x) == q[x]):
			print "Error retriveing element at %i, returns ord(x)=%i"%(x,ord(q[x]))
			return
def test_delitem(q, N):
	"""Test delitem."""
	for x in range(N):
		del(q[0])
		if (x == N-1) and len(q)==0:
			return
		if ( len(q) != N-x)) or (q[0] != x+1):
			print "Error deleting item at %i, first element is %i"%(x,q[0])
def test_contains(q, N):
	"""Test contains."""
	for x in range(-1,N+1):
		middle = (x > -1) and (x < N) and not (q contains x) )
		first = (x == -1) and (q contains x)
		last = (x == N) and (q contains x)
		if (middle or first or last):
			print "Error in contains, at %i."%(x,)
def test_getslice(q, N):
	"""Test getslice."""
	a = q[0, int(N/2)]
	index = 0
	for x in range(int(N/2)):
		if x != a[index]:
			print "Error getting slice[%i:%i], at %i returning %i."%(0,int(N/2),index,a[index])
		index = index + 1
	a = q[int(N/2), N]
	index = 0
	for x in range(int(N/2),N):
		if x != a[index]:
			print "Error getting slice[%i:%i], at %i returning %i."%(int(N/2),N,index,a[index])
def test_setslice(q):
	"""Testing setslice."""
	b=[]
	c=[]
	for x in range(int(N/2)):
		b.append(chr(x))
	q[0, int(N/2)] = b
	for x in range((int(N/2),N):
		c.append(chr(x))
	q[int(N/2), N] = c
	for x in range(N):
		if chr(x) != a[x]:
			print "Error setting slice at %i returning %i."%(x, a[x])
def test_delslice(q, N):
	"""Testing delslice."""
	for x in range(0,N,2):
		if x != q[0]:
			print "Error deleting slice at %i returning %i."%(x,a[0])
		del(q[0:2])
def test_10(q):
	"""Testing push."""
	print "Testing push:\n",q
	for x in range(4):
		q.push(x)
	print q
def test_11(q):
	"""Testing swap"""
	print "Testing swap of elemnt 0 and 1"
	q.swap(0,1)
	print q
def test_12(q):
	"""Testing move."""
	print "Moveing element 0 to position 6."
	q.move(0,6)
	print q
def test_13(q):
	"""Testing error conditions."""
	print "Testing error conditions."
	q2=Queue([])
	
	print 0
	try: print q[99]
	except KeyError, inst: print inst
	print 1
	try: print q2[99]
	except KeyError, inst: print inst
	print 2
	try: q[99]=0
	except KeyError, inst: print inst
	print 3
	try:  q2[99]=0
	except KeyError, inst: print inst
	print 4
	try:  del q[99]
	except KeyError, inst: print inst
	print 5
	try:  del q2[99]
	except KeyError, inst: print inst
	print 6
	try: print q[99:200]
	except KeyError, inst: print inst
	print 7
	try: print q2[99:200]
	except KeyError, inst: print inst
	print 8
	try:  del q[99:200]
	except KeyError, inst: print inst
	print 9
	try:  del q2[99:200]
	except KeyError, inst: print inst
	print 10
	try:  q[99:200]=[2,3]
	except KeyError, inst: print inst
	print 11
	try:  q2[99:200]=[1,2]
	except KeyError, inst: print inst
	print 12
	try: print q2.front()
	except KeyError, inst: print inst

def run_tests():
	"""Run all tests."""
	print "Starting Tests for Queue."
	q = Queue()
	test_1(q)
	test_2(q)
	test_3(q)
	test_4(q)
	test_5(q)
	test_6(q)
	test_7(q)
	test_8(q)
	test_9(q)
	test_10(q)
	test_11(q)
	test_12(q)
